/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is
 *
 * java-concurrency-presentation:
 * concurrency presentation tutorial.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): None.
 *
 * ***** END LICENSE BLOCK *****
 */

package presentation.concurrency.indexer.simple;

import presentation.concurrency.indexer.Index;
import presentation.concurrency.indexer.Indexer;
import presentation.concurrency.indexer.Location;
import presentation.concurrency.indexer.Word;
import presentation.concurrency.util.ContextSwitcher;

import java.io.*;
import java.util.*;

/**
 * An example of how to index documents through brute force
 */
public class BruteForceNonConcurrentIndexer extends Indexer {

    private class IndexImpl implements Index {
        private Map<String, Word> values = new TreeMap<String, Word>();
        private long wordCount = 0;
        private ContextSwitcher contextSwitcher = new ContextSwitcher();
        public void add(String word, String file, int line) {

            Word previous = values.get(word);
            if (previous!=null) {
                previous.add(file, line);
            } else {
                Word current = new WordImpl();
                current.add(file, line);
                values.put(word, current);
            }
            //wordCount++;
            long w = wordCount + 1;
            contextSwitcher.forceSometimes();
            wordCount = w;
        }
        public Set<Map.Entry<String, Word>> getWords() {
            return values.entrySet();
        }
        public long getWordCount() {
            return wordCount;
        }
    }

    private class WordImpl implements Word {
        private long count = 0;
        private Set<Location> locations = new LinkedHashSet<Location>();
        public void add(String file, int line) {
            locations.add(new Location(file, line));
            count++;
        }
        public long getCount() {
            return count;
        }
        public Set<Location> getLocations() {
            return locations;
        }
    }

    public Index execute(File[] files) {

        Index index = new IndexImpl();
        for (File file : files) {
            index(index, file);            
        }
        return index;
    }
}
